using System;
using System.Collections.Generic;
using System.Text;
using usm.SolDev.Common.Constants;
using System.ComponentModel;
using System.Xml.Serialization;
using usm.SolDev.Common;

namespace usm.SolDev.DataBase.DataObjects
{
    [Serializable]
    public class ProjectDescriptor: DataObject
    {
        #region Private fields
        private Guid id;
        private string program;
        private string results;
        private string entities;
        private int userId;
        private int mark;
        private string description;
        private string userName;
        private Application.Origins origin;
        private string groupName;
        private DateTime lastEditTime;
        private string filePath;
        #endregion

        #region Constructors
        /// <summary>
        ///     default constructor
        /// </summary>
        public ProjectDescriptor () {}

        /// <summary>
        ///     Setup program descriptor
        /// </summary>
        /// <param name="id">Program GUID </param>
        /// <param name="program">program text</param>
        /// <param name="results">results to store in the database</param>
        /// <param name="userId">user id </param>
        /// <param name="mark">teacher's mark</param>
        public ProjectDescriptor(Guid id, string program, string results, string entities, int userId, int mark, string description, DateTime lastEditTime, string userName, string groupName)
        {
            this.id = id;
            this.program = program;
            this.results = results;
            this.entities = entities;
            this.userId = userId;
            this.mark = mark;
            this.description = description;
            this.lastEditTime = lastEditTime;
            this.userName = userName;
            this.groupName = groupName;
        }

        /// <summary>
        ///     Generates automatically program ID.
        /// </summary>
        /// <param name="program">program text</param>
        /// <param name="results">results to store in the database</param>
        /// <param name="userId">user id </param>
        /// <param name="mark">teacher's mark</param>
        public ProjectDescriptor(string program, string results, string entities, int userId, int mark, string description, DateTime lastEditTime, string userName, string groupName)
        {
            this.id = Guid.NewGuid();
            this.program = program;
            this.results = results;
            this.entities = entities;
            this.userId = userId;
            this.mark = mark;
            this.description = description;
            this.lastEditTime = lastEditTime;
            this.userName = userName;
            this.groupName = groupName;
        }

        #endregion

        #region Public properties
        /// <summary>
        ///     Program GUID
        /// </summary>
        [CategoryAttribute("Program description"), DescriptionAttribute("The program unique ID"), BrowsableAttribute(true), ReadOnlyAttribute(true)]
        [XmlIgnore]
        public Guid Id
        {
            get { return id; }
            set { id = value; }
        }


        [BrowsableAttribute(false)]
        [XmlAttributeAttribute(DataType = "string")]
        public string XmlId
        {
            get { return id.ToString(); }
            set { id = new Guid(value); }
        }

        /// <summary>
        ///     Program text (listing)
        /// </summary>
        [BrowsableAttribute(false)]
        [XmlIgnore]
        public string Program
        {
            get { return program; }
            set { program = value; }
        }

        /// <summary>
        ///     The encoded program to store
        /// </summary>
        [XmlAttributeAttribute(DataType = "string")]
        [BrowsableAttribute(false)]
        public string XmlProgram
        {
            get { return Cryptography.Base64Encode(program); }
            set { program = Cryptography.Base64Decode(value); }
        }

        /// <summary>
        ///     Program execution results
        /// </summary>
        [BrowsableAttribute(false)]
        [XmlIgnore]
        public string Results
        {
            get { return results; }
            set { results = value; }
        }

        /// <summary>
        ///     Program execution results
        /// </summary>
        [XmlAttributeAttribute(DataType = "string")]
        [BrowsableAttribute(false)]
        public string XmlResults
        {
            get { return Cryptography.Base64Encode(results); }
            set { results = Cryptography.Base64Decode(value); }
        }

        /// <summary>
        ///     The entities found in source file (variables, processes, tables, facilities ...)
        /// </summary>
        [BrowsableAttribute(false)]
        [XmlIgnore]
        public string Entities
        {
            get { return entities; }
            set { entities = value; }
        }

        /// <summary>
        ///     The entities found in source file (variables, processes, tables, facilities ...)
        /// </summary>
        [XmlAttributeAttribute(DataType = "string")]
        [BrowsableAttribute(false)]
        public string XmlEntities
        {
            get { return Cryptography.Base64Encode(entities); }
            set { entities = Cryptography.Base64Decode(value); }
        }

        /// <summary>
        ///     Owner Id
        /// </summary>
        [BrowsableAttribute(false)]
        [XmlAttributeAttribute(DataType = "int")]
        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        /// <summary>
        ///     Teacher's mark
        /// </summary>
        [CategoryAttribute("Program description"), DescriptionAttribute("The achieved mark"), BrowsableAttribute(true), ReadOnlyAttribute(true)]
        [XmlAttributeAttribute(DataType = "int")]
        public int Mark
        {
            get { return mark; }
            set { mark = value; }
        }

        /// <summary>
        ///     The short description of the program
        /// </summary>
        [CategoryAttribute("Program description"), DescriptionAttribute("The program description"), BrowsableAttribute(true), ReadOnlyAttribute(true)]
        [XmlAttributeAttribute(DataType = "string")]
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        /// <summary>
        ///     The user name to which this project exits.
        /// </summary>
        [CategoryAttribute("Author"), DescriptionAttribute("The author's name"), BrowsableAttribute(true), ReadOnlyAttribute(true)]
        [XmlAttributeAttribute(DataType = "string")]
        public string UserName
        {
            get { return userName; }
            set { userName = value; }

        }

        /// <summary>
        ///     Describe the project origin. The save and save as code 
        /// will know where to place project.
        /// </summary>
        [BrowsableAttribute(false), ReadOnlyAttribute(true)]
        [XmlIgnore]
        public Application.Origins Origin
        {
            get { return origin; }
            set { origin = value; }
        }

        /// <summary>
        ///     The  name of the group 
        /// </summary>
        [CategoryAttribute("Author"), DescriptionAttribute("The author's group"), BrowsableAttribute(true), ReadOnlyAttribute(true)]
        [XmlAttributeAttribute(DataType = "string")]
        public string GroupName
        {
            get { return groupName; }
            set { groupName = value; }
        }

        /// <summary>
        ///     Hold the last edit date time of the project
        /// </summary>
        [CategoryAttribute("Program description"), DescriptionAttribute("The last edit time"), BrowsableAttribute(true), ReadOnlyAttribute(true)]
        [XmlAttributeAttribute(DataType = "time")]
        public DateTime LastEditTime
        {
            get { return lastEditTime; }
            set { lastEditTime = value; }
        }

        /// <summary>
        ///     Hold's the file path. If the file has been saved to 
        /// local file system, or opened from a local file system
        /// </summary>
        [BrowsableAttribute(false), ReadOnlyAttribute(true)]
        [XmlIgnore]
        public string FilePath
        {
            get { return filePath; }
            set { filePath = value; }
        }
        #endregion
    }
}
